/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.program;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;

import com.google.gwt.user.client.History;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.grid.HoverCustomizer;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.MenuItemIfFunction;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;
import com.smartgwt.client.widgets.tree.events.LeafClickEvent;
import com.smartgwt.client.widgets.tree.events.LeafClickHandler;

public class AppTreeGrid extends TreeGrid
{

	public static final String usertemplateText = "usertemplate";
	public static final String globaltemplateText = "globaltemplates";
	public static final String applicationText = "applications";

	public static final int usertemplateId = 0;
	public static final int globaltemplateId = 1;
	public static final int applicationId = 2;

	public AppTreeGrid()
	{
		setWidth(250);
		setHeight100();
		setShowRoot(false);

//		setShowEdges(true);
//		setBorder("opx");
//		setBodyStyleName("normal");
		setAnimateFolders(false);

		TreeNode[] roots = new TreeNode[3];

		TreeNode usertemplate = new TreeNode();
		usertemplate.setAttribute("id", usertemplateId);
		usertemplate.setAttribute("name", usertemplateText);
		usertemplate.setAttribute("text", "User Templates");
		// IF this is used, the tree is not loadOnDemand any more.
		// usertemplate.setIsFolder(true);
		usertemplate.setAttribute("folder", true);
		roots[0] = usertemplate;

		TreeNode globaltemplate = new TreeNode();
		globaltemplate.setAttribute("id", globaltemplateId);
		globaltemplate.setAttribute("name", globaltemplateText);
		globaltemplate.setAttribute("text", "Global Templates");
		globaltemplate.setAttribute("folder", true);
		roots[1] = globaltemplate;

		TreeNode apps = new TreeNode();
		apps.setAttribute("id", applicationId);
		apps.setAttribute("name", applicationText);
		apps.setAttribute("text", "Applications");
		apps.setAttribute("folder", true);
		roots[2] = apps;

		TreeGridField tf = new TreeGridField("text", "Applications");
		tf.setCanEdit(false);
		tf.setCanToggle(false);
		tf.setCanSort(true);
		tf.setTreeField(true);
		tf.setShowHover(true);
		tf.setHoverCustomizer(new HoverCustomizer()
		{

			public String hoverHTML(Object value, ListGridRecord record, int rowNum, int colNum)
			{
				return record.getAttribute("tip") == null ? record.getAttribute("text") : record.getAttribute("tip");

			}
		});

		setFields(tf);
		setInitialData(roots);
		this.setShowResizeBar(true);

		final AppTreeDataSource src = new AppTreeDataSource();
		setDataSource(src);
		setLoadDataOnDemand(true);

		addLeafClickHandler(new LeafClickHandler()
		{

			public void onLeafClick(LeafClickEvent event)
			{
				TreeNode node = event.getLeaf();
				StaticHolder.loadingWindow.show("loading application " + event.getLeaf().getAttribute("text") + "...", true);
				History.newItem("appnode_" + node.getAttribute("ptype") + "_" + node.getAttribute("pvalue"));
			}
		});

		addDrawHandler(new DrawHandler()
		{
			public void onDraw(DrawEvent event)
			{
				Tree tree = AppTreeGrid.this.getTree();
				src.setTree(tree);
				tree.setIsFolderProperty("folder");
				tree.setNameProperty("name");
				tree.setPathDelim("/");
				tree.getRoot().setAttribute("name", "");
			}
		});
		final Menu menu = new Menu();
		menu.setShowShadow(true);
		menu.setShadowDepth(10);

		final MenuItem reloadItem = new MenuItem("Reload");
		// --------------reload
		reloadItem.addClickHandler(new ClickHandler()
		{

			public void onClick(MenuItemClickEvent event)
			{
				TreeNode treeNode = (TreeNode) AppTreeGrid.this.getSelectedRecord();
				if (!treeNode.getAttributeAsBoolean("folder"))
				{
					treeNode = AppTreeGrid.this.getTree().getParent(treeNode);
				}
				AppTreeGrid.this.getTree().reloadChildren(treeNode);
			}
		});
		reloadItem.setEnableIfCondition(new MenuItemIfFunction()
		{

			public boolean execute(Canvas target, Menu menu, MenuItem item)
			{
				TreeNode node = (TreeNode) AppTreeGrid.this.getRecord(AppTreeGrid.this.getEventRow());
				return node.getAttributeAsBoolean("folder");
			}
		});
		menu.setItems(reloadItem);

		setContextMenu(menu);

	}
}
